home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / bin / os-prober < prev    next >
Text File  |  2008-08-25  |  4KB  |  140 lines

  1. #!/bin/sh
  2. set -e
  3.  
  4. . /usr/share/os-prober/common.sh
  5.  
  6. require_tmpdir
  7.  
  8. log_output () {
  9.     if type log-output >/dev/null 2>&1; then
  10.         log-output -t os-prober --pass-stdout $@
  11.     else
  12.         $@
  13.     fi
  14. }
  15.  
  16. on_sataraid () {
  17.     type dmraid >/dev/null 2>&1 || return 1
  18.     local parent=${1%/*}
  19.     local device=/dev/${parent##*/}
  20.     if dmraid -r -c | grep -q $device; then
  21.         return 0
  22.     fi
  23.     return 1
  24. }
  25.  
  26. partitions () {
  27.     # Exclude partitions that have whole_disk sysfs attribute set.
  28.     # Exclude partitions on physical disks that are part of a Serial
  29.     # ATA RAID disk. TODO: add support for the SATA RAID disk itself.
  30.     if [ -d /sys/block ]; then
  31.         for part in /sys/block/*/*[0-9]; do
  32.             if [ -f "$part/start" ] && \
  33.                [ ! -f "$part/whole_disk" ] && ! on_sataraid $part; then
  34.                 name="$(echo "${part##*/}" | sed 's,[!.],/,g')"
  35.                 if [ -e "/dev/$name" ]; then
  36.                     echo "/dev/$name"
  37.                 fi
  38.             fi
  39.         done
  40.  
  41.         if type dmraid >/dev/null; then
  42.             for raidset in $(dmraid -sa -c | grep -v "No RAID disks"); do
  43.                 for part in /dev/mapper/$raidset*[0-9]; do
  44.                     echo "$part"
  45.                 done
  46.             done
  47.         fi
  48.     else
  49.         echo "Cannot find list of partitions!" >&2
  50.         exit 1
  51.     fi
  52.  
  53.     # Also detect OSes on LVM volumes (assumes LVM is active)
  54.     if type lvs >/dev/null 2>&1; then
  55.         echo "$(log_output lvs --noheadings --separator : -o vg_name,lv_name |
  56.             sed "s|-|--|g;s|^[[:space:]]*\(.*\):\(.*\)$|/dev/mapper/\1-\2|")"
  57.     fi
  58. }
  59.  
  60. parse_proc_mdstat () {
  61.     if type udevadm >/dev/null 2>&1; then
  62.         udevinfo () {
  63.             udevadm info "$@"
  64.         }
  65.     fi
  66.     while read line; do
  67.         for word in $line; do
  68.             dev="${word%%[*}"
  69.             # TODO: factor this out to something in di-utils if
  70.             # it's needed elsewhere
  71.             if [ -d /sys/block ] && type udevinfo >/dev/null 2>&1; then
  72.                 if ! udevinfo -q path -n "/dev/$dev" 2>/dev/null | \
  73.                      grep -q '/.*/.*/'; then
  74.                     continue
  75.                 fi
  76.             elif ! echo "$dev" | grep -q "/part"; then
  77.                 continue
  78.             fi
  79.             raidpart="/dev/$dev"
  80.             echo "$(mapdevfs $raidpart)"
  81.         done
  82.     done
  83. }
  84.  
  85. # Needed for idempotency
  86. rm -f /var/lib/os-prober/labels
  87.  
  88. for prog in /usr/lib/os-probes/init/*; do
  89.     if [ -x $prog ] && [ -f $prog ]; then
  90.         $prog || true
  91.     fi
  92. done
  93.  
  94. # We need to properly canonicalize partitions with mount points and partitions
  95. # used in RAID
  96. grep "^/dev/" /proc/mounts | parse_proc_mounts >"$OS_PROBER_TMP/mounted-map" || true
  97. : >"$OS_PROBER_TMP/raided-map"
  98. if [ -f /proc/mdstat ] ; then
  99.     grep "^md" /proc/mdstat | parse_proc_mdstat >"$OS_PROBER_TMP/raided-map" || true
  100. fi
  101.  
  102. for partition in $(partitions); do
  103.     if ! mapped=$(mapdevfs $partition); then
  104.         log "Device '$partition' does not exist; skipping"
  105.         continue
  106.     fi
  107.  
  108.     # Skip partitions used in software RAID arrays
  109.     if grep -q "^$mapped" "$OS_PROBER_TMP/raided-map" ; then
  110.         debug "$partition: part of software raid array"
  111.         continue
  112.     fi
  113.  
  114.     if ! grep -q "^$mapped " "$OS_PROBER_TMP/mounted-map" ; then
  115.         for test in /usr/lib/os-probes/*; do
  116.             if [ -f $test ] && [ -x $test ]; then
  117.                 debug "running $test on $partition"
  118.                 if $test $partition; then
  119.                     debug "os detected by $test"
  120.                        break
  121.                 fi
  122.             fi
  123.         done
  124.     else
  125.         mpoint=$(grep "^$mapped " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 2)
  126.         if [ "$mpoint" != "/target/boot" ] && [ "$mpoint" != "/target" ] && [ "$mpoint" != "/" ]; then
  127.             type=$(grep "^$mapped " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 3)
  128.             for test in /usr/lib/os-probes/mounted/*; do
  129.                 if [ -f $test ] && [ -x $test ]; then
  130.                     debug "running $test on mounted $partition"
  131.                     if $test $partition $mpoint $type; then
  132.                         debug "os detected by $test"
  133.                         break
  134.                     fi
  135.                 fi
  136.             done
  137.         fi
  138.     fi
  139. done
  140.